package org.example.service.acceptMessageImpl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dao.ProblemDao;
import org.example.dao.SubmissionsDao;
import org.example.fegin.JudgeFegin;
import org.example.pojo.Request.CodeEntity;
import org.example.pojo.Request.RunCodeEntity;
import org.example.pojo.Response.JudgeResult;
import org.example.pojo.Submissions;
import org.example.rabbitmq.sendUtil;
import org.example.service.acceptMessage;
import org.example.service.handleJudgeResult;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * 处理消息体
 */
@Service("org.example.service")
public class acceptMessageImpl implements acceptMessage {

    @Autowired
    private sendUtil<CodeEntity> sendUtil;

    @Autowired
    @Lazy
    private handleJudgeResult handleJudgeResult;

    @Autowired
    private SubmissionsDao submissionsDao;
   @Autowired
    private JudgeFegin judgeFegin;


    /**
     * 提交代码
     * @param message
     * @param userId
     * @return
     */
    @Override
    public Boolean acceptMessage(String message,String userId) {

        return sendSubmission(message, userId);

    }

    /**
     * 运行测试用例
     * @param message
     * @param userId
     * @return
     */

    @Override
    public Boolean acceptRunCodeMessage(String message, String userId) {


         CompletableFuture.supplyAsync(() -> {
            //openfegin 调取判题结果
             return judgeFegin.RunCodeJudge(message, userId);
        }).thenAccept((a) -> {

            //封装返回结果

             handleJudgeResult.CaseResult(a);
        });
        System.out.println(6666);

        return true;
    }


    @NotNull
    private Boolean sendSubmission(String message, String userId) {
        if (message != null) {
            try {
                // 将message转换为CodeEntity对象
                CodeEntity codeEntity = JSONUtil.toBean(message, CodeEntity.class);
                if (codeEntity != null) {
                    //提交数据到数据库
                    Submissions submission = Submissions.builder()
                            .problemId(codeEntity.getProblemID())
                            .userId(userId)
                            .submittedCode(codeEntity.getSourceCode())
                            .build();
                    submissionsDao.insert(submission);
                    //获取ID
                    Integer id = submissionsDao.selectOne(new QueryWrapper<>(submission)).getId();

                    codeEntity.setSubmissionID(id);
                    // 调用sendProblem方法，将codeEntity作为参数传递
                    sendUtil.sendProblem(codeEntity);
                }
            } catch (Exception e) {
                // 处理JSON转换异常
                e.printStackTrace();
                //TODO 异常处理机制
                return false;
            }
        }
        return true;
    }




}
